package com.xkdx.guangguang;

import java.io.File;
import java.io.FileNotFoundException;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;

import com.xkdx.caipiao.R;
import com.xkdx.guangguang.fragment.user.UserLoginSuccessFragment;
import com.xkdx.guangguang.shareclass.UpLoadHead;
import com.xkdx.guangguang.shareclass.User;

public class UpLoadHeadPhotoLoginActiivty extends ManagerActivity {
	// 增加登陆成功修改头像弹出拍照，相册 获取照片问题
	public static final int NONE = 0;
	public static final int LOGIN_PHOTOHRAPH = 10;// 拍照
	public static final int LOGIN_PHOTOZOOM = 20; // 缩放
	public static final int LOGIN_PHOTORESOULT = 30;// 结果
	public static String ImageNameLogin;

	Bitmap photo;
	public static boolean isUpLoadSuccess = false;
	User user;
	UpLoadHead upLoadHead;
	String IMAGE_UNSPECIFIED = "image/*";
	Uri uri;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.upload_photo_choice);
	}

	// @Override
	// public boolean onTouchEvent(MotionEvent event) {
	// finish();
	// return true;
	// }

	public void exitbutton1(View v) {

		getNativePhotoFromLogin();
	}

	public void exitbutton0(View v) {

		getTakePhotoFromLogin();
	}

	public void exitbutton(View v) {
		finish();
	}

	@Override
	public void onBackPressed() {
		finish();
	}

	// 成功登陆页面点击头像获取照相图片
	public void getTakePhotoFromLogin() {
		ImageNameLogin = "head.jpg";
		Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
		File file = new File(Environment.getExternalStorageDirectory() + "/GuangGuang/user");
		if (!file.exists()) {
			file.mkdirs();
		}
		intent.putExtra(MediaStore.EXTRA_OUTPUT,
				Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/GuangGuang/user", ImageNameLogin)));
		startActivityForResult(intent, LOGIN_PHOTOHRAPH);
	}

	// 成功登陆页面点击头像获取本地图片
	public void getNativePhotoFromLogin() {
		Intent intent = new Intent(Intent.ACTION_PICK, null);
		intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, IMAGE_UNSPECIFIED);
		// 调用剪切功能
		startActivityForResult(intent, LOGIN_PHOTOZOOM);
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode == NONE) {
			return;
		}

		if (requestCode == LOGIN_PHOTOHRAPH) {

			File picture1 = new File(Environment.getExternalStorageDirectory() + "/GuangGuang/user", ImageNameLogin);
			uri = Uri.fromFile(picture1);
			startLoginPhotoZoom(uri, uri);
		}

		if (requestCode == LOGIN_PHOTOZOOM) {
			ImageNameLogin = "head.jpg";
			File picture1 = new File(Environment.getExternalStorageDirectory() + "/GuangGuang/user", ImageNameLogin);
			uri = Uri.fromFile(picture1);
			startLoginPhotoZoom(data.getData(), uri);
		}
		if (data == null) {
			return;
		}
		// 处理结果
		if (requestCode == LOGIN_PHOTORESOULT) {
			UserLoginSuccessFragment.headBitmap = decodeUriAsBitmap(uri);
			finish();
		}
	}

	// 从登陆页面进行拍照，相册处理
	public void startLoginPhotoZoom(Uri uri, Uri endUri) {
		Intent intent = new Intent("com.android.camera.action.CROP");

		intent.setDataAndType(uri, IMAGE_UNSPECIFIED);
		intent.putExtra("crop", "true");
		intent.putExtra("aspectX", 1);
		intent.putExtra("aspectY", 1);
		intent.putExtra("outputX", 300);
		intent.putExtra("outputY", 300);
		intent.putExtra("return-data", false);
		intent.putExtra(MediaStore.EXTRA_OUTPUT, endUri);
		intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
		intent.putExtra("noFaceDetection", true);
		startActivityForResult(intent, LOGIN_PHOTORESOULT);
	}

	private Bitmap decodeUriAsBitmap(Uri uri) {

		Bitmap bitmap = null;

		try {
			bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			return null;

		}

		return bitmap;

	}

}
